package com.lc.ei.service.impl;



import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.lc.ei.config.listener.ExcelListener;
import com.lc.ei.service.DataService;
import com.lc.ei.utils.GenEntityUtil;
import com.study.common.entity.dto.ExportDto;
import com.study.common.utils.NoUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: some desc
 * @author: LChuang
 * @date: 2023/9/1 11:18
 */
@Service
public class DataServiceImpl implements DataService {

    private static final List<ExportDto> list;

    @Resource
    private ExcelListener excelListener;

    static {
        list = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            list.add(ExportDto.builder().no(NoUtil.create()).name("export_data").account("12300" + i).avatar("www.baidu.com").time(LocalDateTime.now()).build());
        }
    }

    @Override
    public List<ExportDto> getExportData() {
        return list;
    }

    @Override
    public void parseData(MultipartFile file) throws IOException {
        // ExcelListener监听器读取数据
        EasyExcel.read(file.getInputStream(), excelListener).sheet().doRead();
        // 英文表头
        List<String> headList = excelListener.getModifyHeadList();
        // 英文数据
        Map<Integer, Map<String, String>> dataMap = excelListener.getModifyDataMap();
        // 调用生成实体工具，根据表头生成对应实体对象
        Object o = GenEntityUtil.memoryGenEntity(headList, GenEntityUtil.genClassName());
        assert o != null;
        for (Map.Entry<Integer, Map<String, String>> dataEntity : dataMap.entrySet()) {
            // map填充bean对象
            Object bean = BeanUtil.fillBeanWithMap(dataEntity.getValue(), o, false);
        }
    }
}
